<template>
    <div>
        <!-- 营收统计信息 -->
        <el-row>
            <el-col :span="10">
                <el-statistic group-separator="," :precision="2" :value="value" :title="title"></el-statistic>
            </el-col>
            <el-col :span="14">
                <el-statistic group-separator="," :precision="2" :value="value2" :title="title2"></el-statistic>
            </el-col>
        </el-row>

        <el-row style="margin-top: 40px;">
            <el-col :span="12">
                <el-descriptions title="当前账号信息">
                    <el-descriptions-item label="用户名">{{ AccountInfoDto.account }}</el-descriptions-item>
                    <el-descriptions-item label="权限角色">
                        <el-tag size="small">{{ AccountInfoDto.role }}</el-tag>
                    </el-descriptions-item>
                    <el-descriptions-item label="所属物业公司">
                        {{ AccountInfoDto.certifiedName }}
                    </el-descriptions-item>
                </el-descriptions>
            </el-col>
            <el-col :span="6">
                <el-button type="success" @click="dialogVisible1 = true" size="mini"
                    style="margin-top: 35px;">新增管理员</el-button>
                <el-button type="primary" @click="dialogVisible = true" size="mini"
                    style="margin-top: 35px;">修改密码</el-button>
                <el-button type="danger" @click="logout" size="mini" style="margin-top: 35px;">注销登录</el-button>
            </el-col>
        </el-row>
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-input placeholder="请输入内容" v-model="UpdatePasswordForm.oldCertifiedAccountPass" clearable>
                <template slot="prepend">旧密码：</template>
            </el-input>
            <el-input placeholder="请输入内容" v-model="UpdatePasswordForm.newCertifiedAccountPass" clearable
                style="margin-top: 20px;">
                <template slot="prepend">新密码：</template>
            </el-input>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="updatePassword">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="提示" :visible.sync="dialogVisible1" width="30%" :before-close="handleClose">
            <el-input placeholder="请输入内容" v-model="RegisterForm.certifiedAccountAccount" clearable>
                <template slot="prepend">账号：</template>
            </el-input>
            <el-input placeholder="请输入内容" v-model="RegisterForm.certifiedAccountPass" clearable style="margin-top: 20px;">
                <template slot="prepend">密码：</template>
            </el-input>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="RegisterAccount">确 定</el-button>
            </span>
        </el-dialog>


        <el-row style="margin-top: 20px;">
            <el-col :span="2">
                <b>所有账号信息:</b>
            </el-col>
        </el-row>

        <el-row style="margin-top: 20px;">
            <el-col>
                <template>
                    <el-table :data="page.list" style="width: 100%">
                        <el-table-column prop="certifiedAccountAccount" label="用户名" width="180">
                        </el-table-column>
                        <el-table-column prop="accountRole" label="权限角色" width="180">
                            <template #default="scope">
                                <span v-if="scope.row.accountRole == 0">超级管理员</span>
                                <span v-if="scope.row.accountRole == 1">管理员</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="certifiedAccountState" label="状态" width="180">
                            <template #default="scope">
                                <span v-if="scope.row.certifiedAccountState == 0">启用</span>
                                <span v-if="scope.row.certifiedAccountState == 1">禁用</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="address" label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="enableAccount(scope.row)">启用</el-button>
                                <el-button type="warning" size="mini" @click="bannedAccount(scope.row)">禁用</el-button>
                                <el-button type="danger" icon="el-icon-delete" size="mini"
                                    @click="deleteAccount(scope.row)"></el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>
            </el-col>
        </el-row>
        <!-- 分页按钮 -->
        <el-row>
            <el-col :span="24">
                <el-pagination background layout="total, prev, pager, next" :page-size="page.size" :total="page.total"
                    @current-change="changePageNum">
                </el-pagination>
            </el-col>
        </el-row>

    </div>
</template>

<script>
export default {
    name: "SystemManage",
    data() {
        return {
            page: {},
            title: "本日营收",
            title2:"本月营收",
            value:"",
            value2:"",
            dialogVisible1: false,
            dialogVisible: false,
            RegisterForm: {
                certifiedAccountAccount: "",
                certifiedAccountPass: ""
            },
            LoadAllAccountForm: {
                pageSize: null,
                pageNum: null,
                certifiedInfoId: null
            },
            UpdatePasswordForm: {
                oldCertifiedAccountPass: "",
                newCertifiedAccountPass: ""
            },
            AccountInfoDto: {
                account: "",
                role: "",
                certifiedName: "",
                certifiedInfoId: null
            }
        }
    },
    methods: {
        cerGetMonthInCome(){
            this.$http.get("/order/orderInfo/cerGetMonthInCome",{
                params:{
                    certifiedInfoId:localStorage.getItem("certifiedInfoId")
                }
            }).then(resp=>{
                console.log(resp)
                if(resp.data.code==200){
                    this.value2=parseFloat(resp.data.data)
                }
            })        
        },
        getTodayIncomeByCerId(){
            this.$http.get("/order/orderInfo/getTodayIncomeByCerId",{
                params:{
                    certifiedInfoId:localStorage.getItem("certifiedInfoId")
                }
            }).then(resp=>{
                console.log(resp)
                if(resp.data.code==200){
                    this.value=parseFloat(resp.data.data)
                }
            })        
        },
        logout() {
            localStorage.removeItem("token")
            localStorage.removeItem("certifiedInfoId")
            this.$router.push("/login")
        },
        RegisterAccount() {
            this.$http.post("/certified/certifiedAccount/RegisterAccount", this.RegisterForm).then(resp => {
                console.log(resp)
                if (resp.data.code == 200) {
                    this.$message({
                        type: 'success',
                        message: resp.data.message
                    });
                    this.cancel()
                    this.loadAllAccount()
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.data.message
                    });
                }
            })
        },
        deleteAccount(obj) {
            this.$confirm('此操作将永久删除该账号, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.get("/certified/certifiedAccount/deleteAccount", {
                    params: {
                        certifiedAccountId: obj.certifiedAccountId
                    }
                }).then(resp => {
                    console.log(resp)
                    if (resp.data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: resp.data.message
                        });
                    } else {
                        this.$message({
                            type: 'warning',
                            message: resp.data.message
                        });
                    }
                    this.loadAllAccount()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        bannedAccount(obj) {
            this.$http.get("/certified/certifiedAccount/bannedAccount", {
                params: {
                    certifiedAccountId: obj.certifiedAccountId
                }
            }).then(resp => {
                if (resp.data.code == 200) {
                    this.$message({
                        type: 'success',
                        message: resp.data.message
                    });
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.data.message
                    });
                }
                this.loadAllAccount()
            })
        },
        enableAccount(obj) {
            this.$http.get("/certified/certifiedAccount/enableAccount", {
                params: {
                    certifiedAccountId: obj.certifiedAccountId
                }
            }).then(resp => {
                if (resp.data.code == 200) {
                    this.$message({
                        type: 'success',
                        message: resp.data.message
                    });
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.data.message
                    });
                }
                this.loadAllAccount()
            })
        },
        changePageNum(pageNum) {
            this.LoadAllAccountForm.pageNum = pageNum;
            this.loadAllAccount();
        },
        loadAllAccount() {
            this.LoadAllAccountForm.certifiedInfoId = localStorage.getItem("certifiedInfoId")
            this.$http.post("/certified/certifiedAccount/loadAllAccount", this.LoadAllAccountForm).then(resp => {
                console.log(resp)
                this.page = resp.data.data
            })
        },
        updatePassword() {
            this.$http.post("/certified/certifiedAccount/updatePassword", this.UpdatePasswordForm).then(resp => {
                console.log(resp)
                alert(resp.data.message)
                if (resp.data.code == 200) {
                    this.cancel()
                }
            })
        },
        cancel() {
            this.UpdatePasswordForm.newCertifiedAccountPass = ""
            this.UpdatePasswordForm.oldCertifiedAccountPass = ""
            this.RegisterForm.certifiedAccountAccount = ""
            this.RegisterForm.certifiedAccountPass = ""
            this.dialogVisible = false
            this.dialogVisible1 = false
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
        },
        getAccountInformation() {
            this.$http.get("/certified/certifiedAccount/getAccountInformation").then(resp => {
                console.log(resp)
                this.AccountInfoDto.account = resp.data.data.account
                this.AccountInfoDto.certifiedInfoId = resp.data.data.certifiedInfoId
                this.AccountInfoDto.certifiedName = resp.data.data.certifiedName
                this.AccountInfoDto.role = resp.data.data.role
                localStorage.setItem("certifiedInfoId", resp.data.data.certifiedInfoId)
            })
            this.loadAllAccount()
        }
    },
    created() {
        this.getAccountInformation()
        this.getTodayIncomeByCerId()
        this.cerGetMonthInCome()

    }
}
</script>

<style scoped></style>